//
//  AdjuestViewController.m
//  QRMaster
//
//  Created by myApple on 15/9/29.
//  Copyright (c) 2015年 myApple. All rights reserved.
//

#import "AdjuestViewController.h"
#import "BackBtn.h"


@interface AdjuestViewController ()<UIGestureRecognizerDelegate>
{
    float lastScale;
    UIImageView * imageView;
    UIImageView * radiusImageView;
    CGRect minImgRect;
    CGRect maxImgRect;
}

@end

@implementation AdjuestViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blackColor];
    lastScale = 1.0;
    [self prepareImageView];
    [self preparGestureRecognizer];
    
    // Do any additional setup after loading the view.
}

-(void)prepareImageView
{
    float ScreenWidth = [UIScreen mainScreen].bounds.size.width;
    float ScreenHeight = [UIScreen mainScreen].bounds.size.height;
    float x = 0;
    float y = 20;
    float width = ScreenWidth;
    float height = 44 ;
    
    UIView  * titleView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    titleView.userInteractionEnabled = YES;
    [self.view addSubview:titleView];
    
    x = 10;
    y = 0;
    height = 40;
    width = 40;
    UIButton * backBtn = [BackBtn initWithFrame:CGRectMake(x, y, width, height) withImage:@"24X24"];
    [backBtn addTarget:self action:@selector(BackClick) forControlEvents:UIControlEventTouchUpInside];
    [titleView addSubview:backBtn];
    
    x = ScreenWidth - 80;
    width = 60;
    height = 40;
    y = 2;
    UIButton * savePictureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    savePictureBtn.frame = CGRectMake(x, y, width, height);
    [savePictureBtn setTitle:@"保存" forState:0];
    savePictureBtn.titleLabel.font = [UIFont systemFontOfSize:13.0];
    [savePictureBtn addTarget:self action:@selector(savePicture:) forControlEvents:UIControlEventTouchUpInside];
    [titleView addSubview:savePictureBtn];
    
    //图片的起始位置
    CGFloat  offFloat = ScreenWidth / _image.size.width;
    width = ScreenWidth / 3.0 * 2;
    height = offFloat * _image.size.height / 3.0 * 2;
    x = (ScreenWidth -width) / 2.0;
    y = (ScreenHeight - height) / 2.0;
    imageView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    imageView.image = _image;
    imageView.userInteractionEnabled = YES;
    imageView.multipleTouchEnabled = YES;
    imageView.layer.borderColor = [[UIColor lightGrayColor]CGColor];
    imageView.layer.borderWidth = 1.0;
    
    minImgRect = imageView.frame;
    maxImgRect = CGRectMake(0, (ScreenHeight - 64) / 2.0 - ScreenWidth / 2.0, ScreenWidth, ScreenWidth);
    [self.view insertSubview:imageView belowSubview:titleView];
//    [self.view addSubview:imageView];
    
    //中间圆孔的固定位置
    x = ScreenWidth / 2.0 - 80;
    y = ScreenHeight / 2.0 -80;
    width = 160;
    height = 160;
    radiusImageView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    radiusImageView.layer.masksToBounds = YES;
    radiusImageView.layer.cornerRadius = 80;
    radiusImageView.clipsToBounds = YES;
    radiusImageView.backgroundColor = [UIColor clearColor];
    radiusImageView.layer.borderColor = [[UIColor whiteColor]CGColor];
    radiusImageView.layer.borderWidth = 2.0;
    radiusImageView.backgroundColor = [UIColor clearColor];
    radiusImageView.userInteractionEnabled = NO;
    [self.view addSubview:radiusImageView];
    
    
    
    //圆孔的边缘都是黑色透明
    //上部的view
    x = 0;
    y = 0;
    height = ScreenHeight / 2.0 - 80 - y;
    width = ScreenWidth;
    UIView * topView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    topView.backgroundColor = [UIColor blackColor];
    topView.alpha = 0.63;
    topView.userInteractionEnabled = NO;
    [self.view insertSubview:topView belowSubview:titleView];
    
    //左边的view
    x = 0;
    y = ScreenHeight / 2.0 - 80;
    height = ScreenHeight- y;
    width = ScreenWidth / 2.0 - 80;
    UIView * leftView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    leftView.backgroundColor = [UIColor blackColor];
    leftView.alpha = 0.63;
    leftView.userInteractionEnabled = NO;
    [self.view addSubview:leftView];
    
    //右边的view
    x = ScreenWidth / 2.0 + 80;
    y = ScreenHeight / 2.0 - 80;
    height = ScreenHeight- y;
    width = ScreenWidth - x;
    UIView * rightView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    rightView.backgroundColor = [UIColor blackColor];
    rightView.alpha = 0.63;
    rightView.userInteractionEnabled = NO;
    [self.view addSubview:rightView];
    
    //下边的view
    x = ScreenWidth / 2.0 - 80;
    y = ScreenHeight / 2.0 + 80;
    height = ScreenHeight - y;
    width = 160;
    UIView * downView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    downView.backgroundColor = [UIColor blackColor];
    downView.alpha = 0.63;
    downView.userInteractionEnabled = NO;
    [self.view addSubview:downView];
}

-(void)preparGestureRecognizer
{
    //捏合缩放
    UIPinchGestureRecognizer * pinchRecognizer = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(scale:)];
    [pinchRecognizer setDelegate:self];
    [imageView addGestureRecognizer:pinchRecognizer];
    //移动
    UIPanGestureRecognizer * panRecognizer =[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(move:)];
    [panRecognizer setDelegate:self];
    [imageView addGestureRecognizer:panRecognizer];
}

//缩放
-(void)scale:(UIPinchGestureRecognizer *)pinch
{
    if ([pinch state] == UIGestureRecognizerStateEnded) {
        lastScale = 1.0;
        //缩放的大小控制
        if (imageView.frame.size.width < 160 || imageView.frame.size.height < 160) {
            imageView.frame = minImgRect;
           
        }
        
//        if (imageView.frame.size.width > [UIScreen mainScreen].bounds.size.width || imageView.frame.size.height > [UIScreen mainScreen].bounds.size.width) {
//            imageView.frame = maxImgRect;
//          
//        }
        return;
    }
    CGFloat scale = 1.0 - (lastScale - [pinch scale]);
    CGAffineTransform currentTransform = imageView.transform;
    CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);
    [imageView setTransform:newTransform];
    lastScale = [pinch scale];
    
}

//移动
-(void)move:(UIPanGestureRecognizer *)pan
{
    CGPoint translation = [pan translationInView:self.view];
    float ScreenWidth = [UIScreen mainScreen].bounds.size.width;
    float ScreenHeight = [UIScreen mainScreen].bounds.size.height;
    if ([pan state] == UIGestureRecognizerStateEnded) {
        //边界控制 不能离开中间的圆框
        if (pan.view.frame.origin.x > ScreenWidth / 2.0 - 80) {
            pan.view.frame = CGRectMake(ScreenWidth / 2.0 - 80, pan.view.frame.origin.y,pan.view.frame.size.width,pan.view.frame.size.height);
        }
        if (pan.view.frame.origin.y > ScreenHeight / 2.0 - 80) {
             pan.view.frame = CGRectMake(pan.view.frame.origin.x, ScreenHeight / 2.0 - 80,pan.view.frame.size.width,pan.view.frame.size.height);
        }
        if (pan.view.frame.origin.x + pan.view.frame.size.width < ScreenWidth / 2.0 + 50) {
             pan.view.frame = CGRectMake(ScreenWidth / 2.0 +80 - pan.view.frame.size.width, pan.view.frame.origin.y,pan.view.frame.size.width,pan.view.frame.size.height);
        }
        if (pan.view.frame.origin.y + pan.view.frame.size.height <  ScreenHeight / 2.0 + 80) {
             pan.view.frame = CGRectMake(pan.view.frame.origin.x, ScreenHeight / 2.0 + 80 - pan.view.frame.size.height,pan.view.frame.size.width,pan.view.frame.size.height);
        }
        return;

    }
    pan.view.center = CGPointMake(pan.view.center.x + translation.x,
                                  pan.view.center.y + translation.y);
    
    [pan setTranslation:CGPointZero inView:self.view];
}

//保存图片（剪切图片）
-(void)savePicture:(UIButton *)btn
{
    UIGraphicsBeginImageContext(imageView.frame.size);
    [_image drawInRect:CGRectMake(0, 0, imageView.frame.size.width, imageView.frame.size.height)];
    UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    UIImage * clipImage = [UIImage imageWithCGImage:CGImageCreateWithImageInRect([newImage CGImage],CGRectMake(radiusImageView.frame.origin.x - imageView.frame.origin.x,radiusImageView.frame.origin.y - imageView.frame.origin.y, 160, 160))];
    if (_aduestViewDelegate && [_aduestViewDelegate respondsToSelector:@selector(AdjuestSendImage:)]) {
        [_aduestViewDelegate AdjuestSendImage:clipImage];
    }
    [self BackClick];
}

-(void)BackClick
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
